<?php

App::uses('AppModel', 'Model');
App::uses('CompositeKeyModel', 'Model');

/**
 * OrganisationContributionType Model
 *
 * @property Organisation $Organisation
 * @property OrganisationMemberContribution $OrganisationMemberContribution
 */
class ModuleContributionType extends CompositeKeyModel {

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $useDbConfig = "organisations";
    public $primaryKeys = array('id', 'organisation_id');

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Organisation' => array(
            'className' => 'Organisation',
            'foreignKey' => 'organisation_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'ModuleMemberContribution' => array(
            'className' => 'ModuleMemberContribution',
            'foreignKey' => 'module_contribution_type_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );


    public function getTypeList($organisation_id = null){
        $data = $this->find('all', array(
            'fields' => array('id', 'name', 'description'),
            'conditions' => array(
                'ModuleContributionType.organisation_id' => $organisation_id,
                'ModuleContributionType.active' => 1
            )
        ));

        if ($data) {
            $list = array();

            foreach ($data as $d) {
                $list[] = $d['ModuleContributionType'];
            }

            return array('status' => 'success', 'lists' => $list);
        }

        return array('status' => 'failed', 'message' => 'No contributions types found for this account.');
    }


    public function getType($organisation_id = null,$module_contribution_type_id = null){
        $data = $this->find('first', array(
            'fields' => array('id', 'name', 'description'),
            'conditions' => array(
                'ModuleContributionType.organisation_id' => $organisation_id,
                'ModuleContributionType.id' => $module_contribution_type_id,
                'ModuleContributionType.active' => 1
            )
        ));

        if ($data) {
            $list = $data['ModuleContributionType'];

            return array('status' => 'success', 'lists' => $list);
        }

        return array('status' => 'failed', 'message' => 'No contributions type found.');
    }


    public function _delete($ids){
        $data =  $this->updateAll(array('ModuleContributionType.active' => '0'), array('ModuleContributionType.id' => $ids));

        if ($data) {

            return array('status' => 'success', 'message' => 'Contribution types deleted!');
        }

        return array('status' => 'failed', 'message' => 'Contribution types cannot be deleted');
    }
}
